const app = getApp()
import Toast from '@vant/weapp/toast/toast'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo:{},
    avatarUrl:''
  },
  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    let that = this
    wx.uploadFile({
      url: app.serverUrl+'/api/upload', 
      name: 'file',
      filePath: avatarUrl,
      method: 'POST',
      header:{
        "Authorization":wx.getStorageSync('token'),
        "X-Requested-With":"XMLHttpRequest",
        "Content-Type":"application/json"
      },
      success(res) {
       res = JSON.parse(res.data)
        // 上传完成需要更新 fileList
       if(res.code==200){
         console.log(res.data.url);
         that.setData({
          'userinfo.avatarIY':res.data.url,
          avatarUrl:res.data.path
        })
        console.log(that.data.userinfo.avatarIY,'222');
       }
      },
    });
    
  },
  onBlur(e){
    this.setData({'userinfo.nickname':e.detail.value})
    console.log(e.detail.value);
  },
  submit(){
    let that = this
    let params = {}
    params.nickname = that.data.userinfo.nickname
    params.avatar = that.data.avatarUrl==''?that.data.userinfo.avatar:that.data.avatarUrl
    console.log(params);
    app.globalData.header.Authorization = wx.getStorageSync('token')
      wx.request({
        url:app.serverUrl+ '/api/avatar',
        header:app.globalData.header,
        method: 'POST',
        data:params,
        success:res=>{
          if(res.data.code==200){
            Toast.success(res.data.message[0])
            setTimeout(()=>{
              wx.navigateBack({
                delta: 1
              })
            },2000)
          }else{
            Toast({
              message:res.data.message[0]
            })
          }
        }
      })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    that.setData({
      userinfo:JSON.parse(options.userinfo)
    })
    console.log(JSON.parse(options.userinfo));
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})